package com.serkol.abmanager.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.serkol.abmanager.bean.GridResult;
import com.serkol.abmanager.bean.Result;
import com.serkol.abmanager.config.ConstantHeader;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.Automobiles;
import com.serkol.abmanager.entity.Goods;
import com.serkol.abmanager.entity.Tracks;
import com.serkol.abmanager.entity.TracksLog;
import com.serkol.abmanager.service.AutomobilesService;
import com.serkol.abmanager.service.GoodsService;
import com.serkol.abmanager.service.TracksLogService;
import com.serkol.abmanager.service.TracksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/track")
@ResponseBody
public class TracksApiController {

    @Autowired
    private TracksService service;
    @Autowired
    private TracksLogService logsService;
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private AutomobilesService automobilesService;

    @PostMapping("/findByList")
    public GridResult findByList(@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "pagesize", defaultValue = "10") int pagesize) {

        GridResult page1 = service.findByList(page, pagesize);

        return page1;
    }

    @PostMapping("/findById")
    public Result findById(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            Tracks byId = service.getById(id);
            if (byId != null) {
                return Result.ok(byId);
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }

    @PostMapping("/findByGoodsId")
    public Result findByGoodsId(@RequestParam(name = "goodsId", defaultValue = "0") long goodsId, @RequestParam(name = "qrCode", defaultValue = "") String qrCode) {
        if (goodsId > 0 || !qrCode.isEmpty()) {
            QueryWrapper<Tracks> queryWrapper = new QueryWrapper<>();
            if (goodsId > 0) {
                queryWrapper.lambda().eq(Tracks::getGoodsId, goodsId);
            }
            if (qrCode.isEmpty()) {
                queryWrapper.lambda().eq(Tracks::getQrCode, qrCode);
            }
            List<Tracks> list = service.list(queryWrapper);

            if (!list.isEmpty()) {
                return Result.ok(list);
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }


    @PostMapping("/findByDelete")
    public Result findByDelete(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            boolean b = service.removeById(id);
            if (b) {
                return Result.ok();
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }

    @PostMapping("/editMore")
    public Result editMore(@RequestParam(name = "id", defaultValue = "0") long id,
                       @RequestParam(name = "carId", defaultValue = "0") long carId,
                       @RequestParam(name = "qrcodes", defaultValue = "") String qrcodes,
                       @RequestParam(name = "boxrfid", defaultValue = "") String boxrfid,
                       @RequestParam(name = "remark", defaultValue = "") String remark,
                       @RequestParam(name = "groupId", defaultValue = "0") long groupId,
                       @RequestParam(name = "deptId", defaultValue = "0") long deptId,
                       @RequestParam(name = "roleId", defaultValue = "0") long roleId) {
        Result result = new Result();
        boolean flag = false;
        if (!qrcodes.isEmpty() && groupId > 0) {
            String[] split = qrcodes.split(",");
            for (int i = 0; i < split.length; i++) {
                String s = split[i];
                flag = false;
                QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
                goodsQueryWrapper.lambda().eq(Goods::getQrCode, s);
                Goods one = goodsService.getOne(goodsQueryWrapper);
                if (one != null) {


                    Tracks goods = new Tracks();
                    goods.setRemark(remark);
                    goods.setDeptId(deptId);
                    goods.setQrCode(s);
                    goods.setGroupId(groupId);
                    goods.setGoodsId(one.getId());
                    goods.setUserId(ConstantHeader.getAdminToken().getUserId());
                    goods.setGoodsId(one.getId());
                    goods.setCarId(carId);
                    goods.setBoxRfid(boxrfid);
                    goods.setRoleId(roleId);
                    goods.setCreateDate(new Date());
                    flag = service.save(goods);
                    if (flag) {
                        Goods goods1 = new Goods();
                        goods1.setId(one.getId());
                        if (!boxrfid.isEmpty()) {
                            goods1.setBoxRfid(boxrfid);
                        }
                        if (carId > 0) {
                            QueryWrapper<Automobiles> carQueryWrapper = new QueryWrapper<>();
                            carQueryWrapper.lambda().eq(Automobiles::getId, carId);
                            Automobiles one1 = automobilesService.getOne(carQueryWrapper);
                            goods1.setLicensePlateNum(one1.getLicensePlateNum());
                            goods1.setCarId(carId);
                        }
                        goods1.setStatus(goods.getId());
                        goodsService.updateById(goods1);
                    }


                } else {

                    result.setStatus(ResultConstant.FAIL);
                    result.setMsg("QRCODE不存在");
                    return result;
                }

            }

            if (flag) {

                result.setStatus(ResultConstant.SUCSESS);
            } else {
                result.setStatus(ResultConstant.FAIL);
            }

        } else {
            result.setMsg("不能为空");
        }

        return result;

    }

    @PostMapping("/edit")
    public Result edit(@RequestParam(name = "id", defaultValue = "0") long id,
                       @RequestParam(name = "carId", defaultValue = "0") long carId,
                       @RequestParam(name = "qrcode", defaultValue = "") String qrcode,
                       @RequestParam(name = "boxrfid", defaultValue = "") String boxrfid,
                       @RequestParam(name = "remark", defaultValue = "") String remark,
                       @RequestParam(name = "groupId", defaultValue = "0") long groupId,
                       @RequestParam(name = "deptId", defaultValue = "0") long deptId,
                       @RequestParam(name = "roleId", defaultValue = "0") long roleId) {
        Result result = new Result();
        if (!qrcode.isEmpty() && groupId > 0) {
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
            goodsQueryWrapper.lambda().eq(Goods::getQrCode, qrcode);
            Goods one = goodsService.getOne(goodsQueryWrapper);
            if (one != null) {

                Tracks goods = new Tracks();
                goods.setRemark(remark);
                goods.setDeptId(deptId);
                goods.setQrCode(qrcode);
                goods.setGroupId(groupId);
                goods.setGoodsId(one.getId());
                goods.setUserId(ConstantHeader.getAdminToken().getUserId());
                goods.setGoodsId(one.getId());
                goods.setCarId(carId);
                goods.setBoxRfid(boxrfid);
                goods.setRoleId(roleId);
                goods.setCreateDate(new Date());

                boolean flag = false;
                long rid = 0;
                if (id > 0) {
                    goods.setId(id);
                    flag = service.updateById(goods);

                } else {
                    flag = service.save(goods);
                    //   System.out.println("RID:"+goods.getId());

                    Goods goods1 = new Goods();
                    goods1.setId(one.getId());
                    if (!boxrfid.isEmpty()) {
                        goods1.setBoxRfid(boxrfid);
                    }
                    if (carId > 0) {
                        QueryWrapper<Automobiles> carQueryWrapper = new QueryWrapper<>();
                        carQueryWrapper.lambda().eq(Automobiles::getId, carId);
                        Automobiles one1 = automobilesService.getOne(carQueryWrapper);
                        goods1.setLicensePlateNum(one1.getLicensePlateNum());
                        goods1.setCarId(carId);
                    }
                    goods1.setStatus(goods.getId());
                    goodsService.updateById(goods1);
                }
                if (flag) {

                    result.setStatus(ResultConstant.SUCSESS);
                } else {
                    result.setStatus(ResultConstant.FAIL);
                }
            } else {
                result.setStatus(ResultConstant.FAIL);
                result.setMsg("QRCODE不存在");
            }
        } else {
            result.setMsg("不能为空");
        }

        return result;

    }

    @PostMapping("/carChange")
    public Result carChange(@RequestParam(name = "id", defaultValue = "0") long id,
                            @RequestParam(name = "newCarId", defaultValue = "0") long newCarId,
                            @RequestParam(name = "oldCarId", defaultValue = "0") long oldCarId,
                            @RequestParam(name = "remark", defaultValue = "") String remark
    ) {
        Result result = new Result();
        if (id > 0) {
            Tracks goods = new Tracks();
            goods.setCarId(newCarId);
            boolean flag = false;
            goods.setId(id);
            flag = service.updateById(goods);

            if (flag) {
                TracksLog tracksLog = new TracksLog();
                tracksLog.setUserId(ConstantHeader.getAdminToken().getUserId());
                tracksLog.setNewCarId(newCarId);
                tracksLog.setOldCarId(oldCarId);
                tracksLog.setTracksId(id);
                tracksLog.setRemark(remark);
                tracksLog.setOpratorId(ConstantHeader.getAdminToken().getUserId());
                boolean save = logsService.save(tracksLog);
                if (save) {

                    result.setStatus(ResultConstant.SUCSESS);
                } else {
                    result.setStatus(ResultConstant.FAIL);
                }
            } else {
                result.setStatus(ResultConstant.FAIL);
            }
        } else {
            result.setMsg("不能为空");
        }

        return result;

    }


    @PostMapping("/logFindByList")
    public Result logFindByList(@RequestParam(name = "id", defaultValue = "0") long id) {


        if (id > 0) {
            QueryWrapper<TracksLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TracksLog::getTracksId, id);
            List<TracksLog> list = logsService.list(queryWrapper);
            if (!list.isEmpty()) {
                return Result.ok(list);
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }
    }

    @PostMapping("/logFindById")
    public Result logFindById(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {

            TracksLog byId = logsService.getById(id);
            if (byId != null) {
                return Result.ok(byId);
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }
}
